package ua.keeper.query;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ClientListAdapter extends ArrayAdapter<ClientListItem>
{
	private ArrayList<ClientListItem> listItems_;
	private Context context_;
    /*****************************************************************************************/
	public ClientListAdapter(Context context, int textViewResourceId, ArrayList<ClientListItem> items)
	{
        super(context, textViewResourceId, items);
        context_ = context;
        listItems_ = items;
	}
	
    /*****************************************************************************************/
	@Override
    public View getView(int position, View convertView, ViewGroup parent)
	{
        View v = convertView;
        if (v == null)
        {
			LayoutInflater vi = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        v = vi.inflate(R.layout.clients_list_row, null);
        }
    	ClientListItem item = (ClientListItem) listItems_.get(position);
        if (item != null)
        {
            TextView tvTitle = (TextView) v.findViewById(R.id.clientTitleView);
            TextView tvCode = (TextView) v.findViewById(R.id.clientCodeView);
            TextView tvTotal = (TextView) v.findViewById(R.id.clientTotalView);
            if (tvTitle != null)
            {
            	tvTitle.setText(item.getTitle());
            }
            if (tvCode != null)
            {
            	tvCode.setText(item.getCode());
            }
            if (tvTotal != null)
            {
            	tvTotal.setText(strPre(item.getClientSumm()) + " (" +
            			Integer.toString(item.getDocCount())+ ")");
            }
        }
        else
        {
			TextView tvTitle = (TextView) v.findViewById(R.id.clientTitleView);
			tvTitle.setText("<ERROR>");
		}
        return v;
    }
	/*****************************************************************************************/
	private String strPre(double inValue)
	{
		DecimalFormat threeDec = new DecimalFormat("0.00");
		threeDec.setGroupingUsed(false);
		return threeDec.format(inValue);
	}
}